<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="150px">
      <el-form-item label="团购名:" prop="title" style="width:80%" >
        <el-input v-model="form.title" placeholder="请输入广告名" />
      </el-form-item>
      <el-form-item label="指定商品:" filterable prop="goods_id">
        <el-select v-model="form.goods_id" placeholder="请选择指定商品">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="团购类型:" prop="type" v-if="super_group_commission>0">
      <el-radio-group v-model="form.type">
        <el-radio v-for="(item,index) in type_arr" :key="index" :label="index">{{item}}</el-radio>
      </el-radio-group>
      <el-tag>超级团购:随机一人成功购买,其它人得分佣金额的{{super_group_commission}}%</el-tag>
      </el-form-item>
      <el-form-item label="封面:" prop="thumb">
        <el-upload
          class="avatar-uploader avatar_item"
          list-type="picture-card"
          :action="upload_url"
          :show-file-list="false"
          :before-upload="$beforeAvatarUpload"
          :on-success="handleAvatarSuccess_thumb"
        >
          <img v-if="thumb" :src="thumb" class="avatar">
          <i v-else class="el-icon-plus" />
        </el-upload>
      </el-form-item>
      <el-form-item label="开始时间:" prop="start_time">
        <el-date-picker
          v-model="form.start_time"
          type="datetime"
          placeholder="选择开始时间"
          default-time="12:00:00"
          @change="changeStartTime()"
        />
      </el-form-item>
      <el-form-item label="结束时间:" prop="end_time">
        <el-date-picker
          v-model="form.end_time"
          type="datetime"
          placeholder="选择结束时间"
          default-time="12:00:00"
          @change="changeEndTime()"
        />
      </el-form-item>
      <el-form-item label="拼团时间(小时):" prop="group_time" style="width:30%">
        <el-input v-model="form.group_time" placeholder="请输入拼团时间" />
      </el-form-item>
      <el-form-item label="价格:" prop="real_price" style="width:30%">
        <el-input v-model="form.real_price" placeholder="请输入价格" />
      </el-form-item>
      <el-form-item label="团购人数:" prop="people_count" style="width:30%">
        <el-input-number v-model="form.people_count" placeholder="请输入团购人数" />
      </el-form-item>
      <el-form-item label="成团数:" prop="group_count" style="width:30%">
        <el-input-number v-model="form.group_count" placeholder="请输入成团数" />
      </el-form-item>
      <el-form-item label="最大成团数:" prop="max_group_count" style="width:30%">
        <el-input-number v-model="form.max_group_count" placeholder="请输入最大成团数" />
      </el-form-item>
      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <el-form-item width='150'>
        <el-button icon="el-icon-close" circle @click="bk()" />
        <el-button
          type="success"
          icon="el-icon-check"
          circle
          @click="onSubmit('form')"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'GroupBuyForm',
    computed:{
    id(){
      return this.$store.state.user.editId
    },

  },
  data() {
    return {
      upload_url: '',
      options: [],
      type_arr: [],
      url: '',
      thumb: '',
      super_group_commission: 0,
      form: {},
      rules: {
        title: [
          { required: true, message: '请输入广告名', trigger: 'blur' }
        ],
        goods_id: [
          { required: true, message: '请选择指定商品', trigger: 'blur' }
        ],
        start_time: [
          { required: true, message: '请选择开始时间', trigger: 'blur' }
        ],
        end_time: [
          { required: true, message: '请选择结束时间', trigger: 'blur' }
        ],
        group_time: [
          { required: true, message: '请输入拼团时间', trigger: 'blur' }
        ],
        real_price: [
          { required: true, message: '请输入价格', trigger: 'blur' }
        ],
        people_count: [
          { required: true, message: '请输入团购人数', trigger: 'blur' }
        ],
        group_count: [
          { required: true, message: '请输入成团数', trigger: 'blur' }
        ],
        max_group_count: [
          { required: true, message: '请输入最大成团数', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
    }
    this.upload_url = process.env.VUE_APP_BASE_API + '/pub/Upload/upload'
    this.init_into()
  },
  methods: {
    changeStartTime() {
      const date = new Date(this.form.start_time)
      this.form.start_time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
    },
    changeEndTime() {
      const date = new Date(this.form.end_time)
      this.form.end_time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
    },
    // 封面上传完成
    handleAvatarSuccess_thumb(res, file) {
      this.form.thumb = file.response.data.path
      this.thumb = file.response.data.path
    },
    // 限制图片大小格式
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)

      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/shop/GoodsGroup/save', form, 'POST').then(e => {this.$store.commit('user/SET_EDIT','')})

        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$store.commit('user/SET_EDIT','')
    },
    // 获取当前数据
    init_into() {
      this.get('/shop/GoodsGroup/info?id=' + this.id, 'POST').then(e => {
        this.options = e.goods
        this.type_arr = e.type_arr
        this.super_group_commission = e.super_group_commission
        const tpl = e.info.tpl
        this.thumb = tpl.thumb
        tpl.status === 1 || tpl.status === '' ? tpl.status = true : tpl.status = false
        this.form = tpl
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
